using System;
using System.Collections.Generic;
using DC2010.Objects.Base;
using System.Xml.Serialization;
using DC2010.Logic;
using DC2010.Objects.Common;
using DC2010.Objects.Structures;

namespace DC2010.Objects.Data
{
    [XmlInclude(typeof(TileTeleportInfo))]
    [Serializable]
    public class TileInfo : BaseInfo
    {
        public TileInfo()
        {
            WallSet = "default";

            //events
            OnEnterEffects = new List<KeyValue>();
            OnLeaveEffects = new List<KeyValue>();

            Items = new List<DungeonPlacement>();
            Loc = new DungeonLoc();

            ObjectsOnTile = new List<ObjectOnTile>();
        }

        /// <summary>
        /// There can be a tile which cannot be passed by monsters
        /// </summary>
        public bool CanMonsterPass { get; set; }
		
		
        /// <summary>
        /// There can be a tile which cannot be passed by player party
        /// </summary>
        public bool CanPlayerPass { get; set; }
	
        public TileType TileType { get; set; }

        public bool HasItems { get; set; }

        
        public string WallSet { get; set; }

        public Directions Direction { get; set; }

        /// <summary>
        /// Are strairs going up?
        /// </summary>
        public bool IsUp { get; set; }

        public List<KeyValue> OnEnterEffects { get; set; } //(I)
        public List<KeyValue> OnLeaveEffects { get; set; } //(I)
        public List<DungeonPlacement> Items { get; set; }

        public DungeonLoc Loc { get; set; }

        /// <summary>
        /// Wall can be broken, doors...
        /// </summary>
        public bool IsBreakable { get; set; }

        public List<ObjectOnTile> ObjectsOnTile { get; set; }

        public virtual TileData CreateData()
        {
            TileData cd = new TileData();

            cd.InfoId = Id;
            cd.Id = cd.InfoId + "_" + Guid.NewGuid().ToString();
            cd.TileType = TileType;
            cd.ObjectsOnTile = new List<ObjectOnTile>();

            foreach (ObjectOnTile oot in ObjectsOnTile)
            {
                //get object we are referencing.
                ObjectReference or = new ObjectReference();
                or.ObjectType = oot.Type;
                or.ReferencedId = oot.ObjectId;

                BaseObject bo = Root.I.GameSystem.GetReferencedObject(or);

                //we have to have bo!=null
                if (bo!=null)
                {
                    switch(oot.Type)
                    {
                        case BaseObjectType.Item:
                            {
                                //so bo should be iteminfo
                                if (bo is ItemInfo)
                                {
                                    ItemInfo ii = (ItemInfo)bo;
                                    ItemData id = ii.CreateData();

                                    //good we instantiated it!
                                    ObjectOnTile ootNew = new ObjectOnTile();
                                    ootNew.ObjectId = id.Id;
                                    ootNew.Position = oot.Position;
                                    ootNew.Type = oot.Type;

                                    cd.ObjectsOnTile.Add(ootNew);
                                }
                                break;
                            }
                    }

                }
            }
            
            //TODO: fill with other properties
            Root.log.InfoFormat("Core, Created new object with id: {0} based on object {1}", cd.Id, cd.InfoId);
            Root.I.TileSystem.AddData(cd);


            return cd;
        }
    }

		// tady je to nejak divne namichany...jsou floor i wall itemy....
	public enum TileType
	{
		None = 0,						// Not defined, possible empty
		Floor = 10,						// Floor
		Wall = 20,						// Non passable wall - roomSTONE = 0
		IllusionWall = 30,				// Passable wall
        Pit = 40,						// roomPIT = 2,
        Stairs = 50,		
        Teleport = 60,
		
		
        PressPadSmall = 70,				// Floor with predefined action....not used in real game....
        PressPadBig = 80,
        TurnLeft = 90,
        TurnRight = 100,
        TurnOposite = 180,
        Generator = 190,
        Blocker = 200,
        SwitchWall = 210,
        FloorDecoration = 220,
        Door = 230,						// roomDOOR = 4
        WallDecoration = 240,
        WallDecorationFountain = 241,
        Button = 250,
        WallDecorationSmall = 260,
        WallDecorationBig = 270,
        Ressurection = 280,
        FlameThrower = 290,
        Alcove = 300,
        Grate  = 310
	}
}        
